/**
 * Created by wangcong (king6cong@gmail.com) on 7/13/16.
 */

package com.yuanxin.player;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.yuanxin.player.media.AndroidMediaController;
import com.yuanxin.player.media.IjkVideoView;

import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

import java.util.LinkedList;

public class YuanXinPlayerView extends FrameLayout {
    private static final String TAG = "RCTIJKPlayerView";
    private IjkVideoView mIJKPlayerView;

    public IjkVideoView getPlayer() {
        return this.mIJKPlayerView;
    }

    //private AndroidMediaController mMediaController;

    private RCTEventEmitter mEventEmitter;

    public YuanXinPlayerView(ThemedReactContext context) {
        super(context);
        mEventEmitter = context.getJSModule(RCTEventEmitter.class);
        IjkMediaPlayer.loadLibrariesOnce(null);
        IjkMediaPlayer.native_profileBegin("libijkplayer.so");
        //mMediaController = new AndroidMediaController(context, false);
        mIJKPlayerView=new IjkVideoView(context.getCurrentActivity());
        //mIJKPlayerView.setMediaController(mMediaController);
        setListener();
        YuanXinPlayer.getInstance().setYuanXinPlayerView(this);
        addView(mIJKPlayerView);
    }

    public void refresh() {
       // Log.e(TAG, "view refresh");
        this.postInvalidate();
        UiThreadUtil.runOnUiThread(new Runnable() {
            public void run() {
                requestLayout();
            }
        });

    }

    private void sendEvent(int state,String eventName) {
       // Log.e(TAG, "sendEvent");
        ReactContext reactContext = (ReactContext) getContext();
        WritableMap params = Arguments.createMap();
        params.putString("state", Integer.toString(state));
        reactContext
                .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit(eventName, params);

    }

    public void start(final String URL) {
       // Log.e(TAG, String.format("start URL %s", URL));
        UiThreadUtil.runOnUiThread(new Runnable() {
            public void run() {
                mIJKPlayerView.setVideoPath(URL);
                mIJKPlayerView.seekTo(0);
                //mIJKPlayerView.setRender(mIJKPlayerView.RENDER_TEXTURE_VIEW);
                mIJKPlayerView.start();
                // RCTIJKPlayerView.this.invalidate();
                // requestLayout();
            }
        });
    }

    public void stop() {
       // Log.e(TAG, String.format("stop"));
        mIJKPlayerView.stopPlayback();
    }

    public void pause() {
       // Log.e(TAG, String.format("pause"));
        mIJKPlayerView.pause();
    }

    public void resume() {
       // Log.e(TAG, String.format("resume"));
        UiThreadUtil.runOnUiThread(new Runnable() {
            public void run() {
                mIJKPlayerView.resume();
            }
        });

    }

    public void shutdown() {
       // Log.e(TAG, String.format("shutdown"));
        mIJKPlayerView.release(true);
    }

    public void seekTo(double currentPlaybackTime) {
        int position = (int) (currentPlaybackTime * 1000);
       // Log.e(TAG, "seekTo " + currentPlaybackTime + ", " + position);
        mIJKPlayerView.seekTo(position);
    }

    public void setListener() {
        mIJKPlayerView.setOnCompletionListener(new IMediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(IMediaPlayer iMediaPlayer) {
                WritableMap event = Arguments.createMap();
                event.putInt("state",0);
                mEventEmitter.receiveEvent(
                        getId(),
                        "onCompletion",
                        event);
               // Log.v(TAG,"onCompletion");
            }
        });

    }
}
